#!/bin/bash
clear
[[ $(awk -F" " '{print $2}' /usr/lib/licence) != "@KIRITO_SSH" ]] && exit 0
fun_bar() {
    comando[0]="$1"
    comando[1]="$2"
    (
        [[ -e $HOME/fim ]] && rm $HOME/fim
        ${comando[0]} -y >/dev/null 2>&1
        ${comando[1]} -y >/dev/null 2>&1
        touch $HOME/fim
    ) >/dev/null 2>&1 &
    tput civis
    echo -ne "\033[1;33m["
    while true; do
        for ((i = 0; i < 18; i++)); do
            echo -ne "\033[1;31m#"
            sleep 0.1s
        done
        [[ -e $HOME/fim ]] && rm $HOME/fim && break
        echo -e "\033[1;33m]"
        sleep 1s
        tput cuu1
        tput dl1
        echo -ne "\033[1;33m["
    done
    echo -e "\033[1;33m]\033[1;37m -\033[1;32m OK !\033[1;37m"
    tput cnorm
}

fun_botOnOff() {
    [[ $(ps x | grep "bot_teste" | grep -v grep | wc -l) = '0' ]] && {
        clear
        echo -e "\E[44;1;37m             INSTALADOR BOT TESTE                \E[0m\n"
        echo -ne "\033[1;32mINFORME SEU TOKEN:\033[1;37m "
        read token
        clear
        echo "-----------MODELO-----------"
        echo "=×=×=×=×=×=×=×=×=×=×=×=×=×=\n"
        echo "   MENSAGEM DE BOAS VINDAS   "
        echo "=×=×=×=×=×=×=×=×=×=×=×=×=×=\n\n"
        echo "        MENSAGEM FINAL         "
        echo ""
        echo -ne "\033[1;32mMENSAGEM DE BOAS VINDAS:\033[1;37m "
        read bvindo
        echo -ne "\033[1;32mMENSAGEM FINAL:\033[1;37m "
        read mfinal
        clear
        echo -ne "\033[1;32mNOME DO BOTÃO 1(GERADOR SSH):\033[1;37m "
        read bt1
        clear
        echo -ne "\033[1;32mNOME DO BOTÃO 2(PERSONALIZADO):\033[1;37m "
        read bt2
        echo -ne "\033[1;32mLINK DO BOTÃO 2:\033[1;37m "
        read link2
        clear
        echo -ne "\033[1;32mNOME DO BOTÃO 3(PERSONALIZADO):\033[1;37m "
        read bt3
        echo -ne "\033[1;32mLINK DO BOTÃO 3:\033[1;37m "
        read link3
        clear
        echo -ne "\033[1;32mDURAÇÃO DO TESTE(EM HORAS):\033[1;37m "
        read dtempo
        clear
        echo ""
        echo -e "\033[1;32mINICIANDO BOT TESTE \033[0m\n"
        fun_bot1() {
            rm -rf $HOME/BOT
            mkdir $HOME/BOT
            [[ ! -e "$HOME/BOT/ShellBot.sh" ]] && {
                wget -qO- https://raw.githubusercontent.com/rodrigo12xd/SSHPLUS/master/Sistema/ShellBot.sh >$HOME/BOT/ShellBot.sh
            }
            [[ ! -e "$HOME/BOT/botssh" ]] && {
                wget -qO- https://raw.githubusercontent.com/rodrigo12xd/SSHPLUS/master/Install/botssh >$HOME/BOT/botssh
            }
            chmod 777 $HOME/BOT/botssh
            chmod 777 $HOME/BOT/ShellBot.sh
            sed -i "s/!#bvindo/$bvindo/g" $HOME/BOT/botssh >/dev/null 2>&1
            sed -i "s/!#mfinal/$mfinal/g" $HOME/BOT/botssh >/dev/null 2>&1
            sed -i "s/!#bt1/$bt1/g" $HOME/BOT/botssh >/dev/null 2>&1
            sed -i "s/!#bt2/$bt2/g" $HOME/BOT/botssh >/dev/null 2>&1
            sed -i "s/!#link2/$link2/g" $HOME/BOT/botssh >/dev/null 2>&1
            sed -i "s/!#bt3/$bt3/g" $HOME/BOT/botssh >/dev/null 2>&1
            sed -i "s/!#link3/$link3/g" $HOME/BOT/botssh >/dev/null 2>&1
            sed -i "s/!#dtempo/$dtempo/g" $HOME/BOT/botssh >/dev/null 2>&1
            cd $HOME/BOT
            screen -dmS bot_teste ./botssh $token >/dev/null 2>&1
            [[ $(grep -wc "bot_teste" /etc/autostart) = '0' ]] && {
                echo -e "ps x | grep 'bot_teste' | grep -v 'grep' || cd $HOME/BOT && sudo screen -dmS bot_teste ./botssh $token && cd $HOME" >>/etc/autostart
            } || {
                sed -i '/bot_teste/d' /etc/autostart
                echo -e "ps x | grep 'bot_teste' | grep -v 'grep' || cd $HOME/BOT/ && sudo screen -dmS bot_teste ./botssh $token && cd $HOME" >>/etc/autostart
            }
        }
        fun_bar 'fun_bot1'
        [[ $(ps x | grep "bot_teste" | grep -v grep | wc -l) != '0' ]] && echo -e "\n\033[1;32m BOT TESTE ATIVADO !\033[0m" || echo -e "\n\033[1;31m ERRO! REANALISE SUAS INFORMACOES\033[0m"
        sleep 2
        menu
    } || {
        clear
        echo -e "\033[1;32mPARANDO BOT TESTE... \033[0m\n"
        fun_bot2() {
            screen -r -S "bot_teste" -X quit
            screen -wipe 1>/dev/null 2>/dev/null
            [[ $(grep -wc "bot_teste" /etc/autostart) != '0' ]] && {
                sed -i '/bot_teste/d' /etc/autostart
            }
            sleep 1
        }
        fun_bar 'fun_bot2'
        echo -e "\n\033[1;32m \033[1;31mBOT TESTE PARADO! \033[0m"
        sleep 2
        menu
    }
}

fun_instbot() {
    echo -e "\E[44;1;37m             INSTALADOR BOT TESTE                \E[0m\n"
    echo -e "                 \033[1;33m[\033[1;31m!\033[1;33m] \033[1;31mATENCAO \033[1;33m[\033[1;31m!\033[1;33m]\033[0m"
    echo -e "\n\033[1;32m1° \033[1;37m- \033[1;33mPELO SEU TELEGRAM ACESSE OS SEGUINTE BOT\033[1;37m:\033[0m"
    echo -e "\n\033[1;32m2° \033[1;37m- \033[1;33mBOT \033[1;37m@BotFather \033[1;33mCRIE O SEU BOT \033[1;31mOPCAO: \033[1;37m/newbot\033[0m"
    echo -e "\033[0;34m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[1;32m"
    echo ""
    read -p "DESEJA CONTINUAR ? [s/n]: " -e -i s resposta
    [[ "$resposta" = 's' ]] && {
        fun_botOnOff
    } || {
        echo -e "\n\033[1;31mRetornando...\033[0m"
        sleep 2
        menu
    }
}
[[ -f "$HOME/BOT/ShellBot.sh" ]] && fun_botOnOff || fun_instbot
#fim
